package com.yllt4cloud.common.core.util;

import com.yllt4cloud.common.core.constant.ErrorCode;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;


/**
 * Created by Administrator on 2015/10/28.
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class ResultResp<T>  implements Serializable {
        private static final long serialVersionUID = 1L;
        private final static Logger logger = LoggerFactory.getLogger(ResultResp.class);
        private String errorMsg = "运行正确";
        private int errorCode = ErrorCode.SUCCESS;
        private T result;
        private int encryptType;

        public int getEncryptType() {
            return encryptType;
        }

        public void setEncryptType(int encryptType) {
            this.encryptType = encryptType;
        }

        public String getErrorMsg() {
            return errorMsg;
        }

        public void setErrorMsg(String errorMsg) {
            this.errorMsg = errorMsg;
        }

        public int getErrorCode() {
            return errorCode;
        }

        public void setErrorCode(int errorCode) {
            this.errorCode = errorCode;
        }

        public T getResult() {
            return result;
        }

        public void setResult(T result) {
            this.result = result;
        }

        public ResultResp(String errorMsg, int errorCode) {
            if(null!=errorMsg){
                this.errorMsg = errorMsg;
            }else {
                this.errorMsg="未知错误";
            }
            this.errorCode = errorCode;
        }
       public ResultResp(String errorMsg, int errorCode, T result) {
        if(null!=errorMsg){
            this.errorMsg = errorMsg;
        }else {
            this.errorMsg="未知错误";
        }
        this.errorCode = errorCode;
        this.result =result;
        }

        public ResultResp(T result) {
            this.result = result;
        }

        public static  < T> ResultResp< T > success()
        {
            return success( null );
        }

        public static < T > ResultResp< T > success(T result )
        {
            return new ResultResp< T >(result);
        }


        public static < T > ResultResp< T > fail(String errorMsg, int errorCode )
        {
            String location="";
            StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
            location = "类名："+stacks[2].getClassName() + ",函数名：" + stacks[2].getMethodName()
                    + ",文件名：" + stacks[2].getFileName() + ",行号："
                    + stacks[2].getLineNumber() + "";
            logger.info(location+","+errorMsg);
            return new ResultResp< T >(errorMsg,errorCode);
        }
        public static < T > ResultResp< T > failed(String errorMsg)
        {
            String location="";
            StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
            location = "类名："+stacks[2].getClassName() + ",函数名：" + stacks[2].getMethodName()
                    + ",文件名：" + stacks[2].getFileName() + ",行号："
                    + stacks[2].getLineNumber() + "";
            logger.info(location+","+errorMsg);
            return new ResultResp< T >(errorMsg,ErrorCode.FAILURE);
        }
        public static < T > ResultResp< T > fail(String errorMsg, int errorCode , T result)
        {
            String location="";
            StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
            location = "类名："+stacks[2].getClassName() + ",函数名：" + stacks[2].getMethodName()
                + ",文件名：" + stacks[2].getFileName() + ",行号："
                + stacks[2].getLineNumber() + "";
            logger.info(location+","+errorMsg);
            return new ResultResp< T >(errorMsg,errorCode,result);
        }

}
